import { Form,Input,Button, message } from 'antd'
import Layout from '../core/Layout'
import axios from 'axios'
import {API} from '../../config'
import { isAuth } from '../../helpers/auth'
import { Link } from 'react-router-dom'

function AddCategory(){
  const {user:{_id},token} = isAuth()
  const handleOnFinish = value =>{
    if(value.name){
      axios.post(`${API}/category/create/${_id}`,value,{
        headers:{
          Authorization:`Bearer ${token}`
        }
      }).then(res=>{
        message.success(`${res.data.name},添加成功`)
      }).catch(err=>{
        console.log(err)
      })
    }
  }
  return (
    <Layout title="添加分类" >
      <Form onFinish={handleOnFinish}>
        <Form.Item label="分类名称" name="name">
          <Input />
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">添加分类</Button>
        </Form.Item>
      </Form>
      <Button>
        <Link to="/admin/dashboard">返回 Dashboard</Link>
      </Button>
    </Layout>
  )
}

export default AddCategory